package com.hdl.servlet.address;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hdl.entity.HdlUser;
import com.hdl.entity.MallAddress;
import com.hdl.service.AddressService;
import com.hdl.service.impl.AddressServiceImpl;

/**
 * 显示某个用户的所有地址信息
 * @author Administrator
 *
 */
@WebServlet("/address.let")
public class AddressListServlet extends HttpServlet{
	
	private AddressService service = new AddressServiceImpl(); 
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//从session中获取当前用户
		HdlUser user = (HdlUser) req.getSession().getAttribute("user");
//		System.out.println(user.getHdlUserId());
		
		if(user == null) {//没有登陆
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}
		
		//存在 调用业务逻辑方法
		List<MallAddress> list = service.getAllByUserId(user.getHdlUserId());
		List<MallAddress> nameList = service.getAllNameByAddress(list);
		//存储数据
		req.setAttribute("addressList", nameList);
		//请求转发
		req.getRequestDispatcher("/address.jsp").forward(req, resp);
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

}
